//
//  RightMVTableViewCellView.m
//  Rongcloud
//
//  Created by LiuLinhong on 2017/08/21.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "RightMVTableViewCellView.h"
#import "CommonUtility.h"

@interface RightMVTableViewCellView ()
{
    BOOL isMouseEntered, isMouseExited;
}

@end


@implementation RightMVTableViewCellView

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        [self initCellView];
        
        isMouseEntered = NO;
        isMouseExited = NO;
        self.isHost = NO;
        self.isOpenMic = YES;
        self.isOpenCamera = YES;
        self.isUpgrade = NO;
        self.isKickOut = NO;
        
        self.micButton.hidden = YES;
        self.cameraButton.hidden = YES;
        self.upgradeButton.hidden = YES;
        self.kickOutButton.hidden = YES;
        
        NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.frame
                                                                    options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow )
                                                                      owner:self
                                                                   userInfo:nil];
        [self addTrackingArea:trackingArea];
    }
    
    return self;
}

- (void)initCellView
{
    self.frame = CGRectMake(0, 0, 200, 44);
    
    self.headImageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 5, 36, 36)];
    self.headImageView.wantsLayer = YES;
    self.headImageView.layer.cornerRadius = 4.0;
    self.headImageView.layer.backgroundColor = [NSColor clearColor].CGColor;
    [self addSubview:self.headImageView];

    self.hostLogoImageView = [[NSImageView alloc] initWithFrame:CGRectMake((self.headImageView.frame.size.width-13)/2, self.headImageView.frame.size.height-13, 13, 13)];
    self.hostLogoImageView.image = [NSImage imageNamed:@"MeetingHostTag"];

    self.userNameTextField = [[NSTextField alloc] initWithFrame:CGRectMake(36, 9, 78, 20)];
    self.userNameTextField.bordered = NO;
    self.userNameTextField.editable = NO;
    self.userNameTextField.bezelStyle = NSTextFieldRoundedBezel;
    self.userNameTextField.alignment = NSTextAlignmentLeft;
    self.userNameTextField.font = [NSFont systemFontOfSize:12.0];
    self.userNameTextField.textColor = [NSColor blackColor];
    self.userNameTextField.backgroundColor = [NSColor greenColor];
    [self addSubview:self.userNameTextField];

    self.micButton = [[NSButton alloc] initWithFrame:CGRectMake(116, 12, 11, 14)];
    self.micButton.bezelStyle = NSBezelStyleTexturedSquare;
    self.micButton.image = [NSImage imageNamed:@"MeetingMenu7MuteGray"];
    [self.micButton setTarget:self];
    [self.micButton setAction:@selector(micButtonPressed:)];
    [self addSubview:self.micButton];
    
    self.cameraButton = [[NSButton alloc] initWithFrame:CGRectMake(135, 12, 11, 14)];
    self.cameraButton.bezelStyle = NSBezelStyleTexturedSquare;
    self.cameraButton.image = [NSImage imageNamed:@"MeetingMenu6CameraGray"];
    [self.cameraButton setTarget:self];
    [self.cameraButton setAction:@selector(cameraButtonPressed:)];
    [self addSubview:self.cameraButton];
    
    self.upgradeButton = [[NSButton alloc] initWithFrame:CGRectMake(155, 12, 11, 14)];
    self.upgradeButton.bezelStyle = NSBezelStyleTexturedSquare;
    self.upgradeButton.image = [NSImage imageNamed:@"MeetingIDUpgrade"];
    [self.upgradeButton setTarget:self];
    [self.upgradeButton setAction:@selector(gradeButtonPressed:)];
    [self addSubview:self.upgradeButton];
    
    self.kickOutButton = [[NSButton alloc] initWithFrame:CGRectMake(174, 13, 12, 12)];
    self.kickOutButton.bezelStyle = NSBezelStyleTexturedSquare;
    self.kickOutButton.image = [NSImage imageNamed:@"MeetingKickOut"];
    [self.kickOutButton setTarget:self];
    [self.kickOutButton setAction:@selector(kickOutButtonPressed:)];
    [self addSubview:self.kickOutButton];
    
    CALayer *lineLayer = [[CALayer alloc] init];
    lineLayer.frame = CGRectMake(0, 0, 200, 0.5);
    lineLayer.backgroundColor = [NSColor lightGrayColor].CGColor;
    self.wantsLayer = YES;
    [self.layer addSublayer:lineLayer];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.isHost = NO;
    self.isOpenMic = YES;
    self.isOpenCamera = YES;
    self.isUpgrade = NO;
    self.isKickOut = NO;
    
    self.micButton.hidden = YES;
    self.cameraButton.hidden = YES;
    self.upgradeButton.hidden = YES;
    self.kickOutButton.hidden = YES;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
}

- (void)viewDidMoveToWindow
{
    [self addTrackingRect:[self bounds] owner:self userData:nil assumeInside:NO];
}

- (void)setIsUpgrade:(BOOL)upgrade
{
    _isUpgrade = upgrade;
    self.upgradeButton.image = _isUpgrade ? [NSImage imageNamed:@"MeetingIDUpgrade"] : [NSImage imageNamed:@"MeetingIDDegrade"];
}

- (void)setIsOpenMic:(BOOL)isOpenMic
{
    _isOpenMic = isOpenMic;
    self.micButton.image = _isOpenMic ? [NSImage imageNamed:@"MeetingMenu7MuteGray"] : [NSImage imageNamed:@"MeetingMenu7MuteBlue"];
}

- (void)setIsOpenCamera:(BOOL)isOpenCamera
{
    _isOpenCamera = isOpenCamera;
    self.cameraButton.image = _isOpenCamera ? [NSImage imageNamed:@"MeetingMenu6CameraGray"] : [NSImage imageNamed:@"MeetingMenu6CameraBlue"];
}

- (void)becomeHost
{
    [self.headImageView addSubview:self.hostLogoImageView];
}

- (void)resignHost
{
    [self.hostLogoImageView removeFromSuperview];
}

- (IBAction)micButtonPressed:(NSButton *)sender
{
    DLog(@"LLH...... micButtonPressed");
    NSString *userID = self.dataDic[kUserID];
    __weak typeof(self) weakSelf = self;
    
    if (self.isOpenMic)
    {
        [self.meetingViewController showMessageAlert:@"确认关闭成员麦克风?" title:@"麦克风" completedBlock:^(BOOL selected) {
            if (selected)
                [weakSelf.meetingViewController controlMemberMicphone:userID status:!weakSelf.isOpenMic];
        }];
    }
    else
        [weakSelf.meetingViewController controlMemberMicphone:userID status:!weakSelf.isOpenMic];
    
    
}

- (IBAction)cameraButtonPressed:(NSButton *)sender
{
    DLog(@"LLH...... cameraButtonPressed");
    NSString *userID = self.dataDic[kUserID];
    __weak typeof(self) weakSelf = self;
    if (self.isOpenCamera)
    {
        [self.meetingViewController showMessageAlert:@"确认关闭成员摄像头?" title:NSLocalizedString(@"meeting_camear", nil) completedBlock:^(BOOL selected) {
            if (selected)
                [weakSelf.meetingViewController controlMemberCamera:userID status:!weakSelf.isOpenCamera];
        }];
    }
    else
        [weakSelf.meetingViewController controlMemberCamera:userID status:!weakSelf.isOpenCamera];
}

- (IBAction)gradeButtonPressed:(NSButton *)sender
{
    DLog(@"LLH...... gradeButtonPressed");
    NSString *userID = self.dataDic[kUserID];
    __weak typeof(self) weakSelf = self;
    if (self.isUpgrade)
        [weakSelf.meetingViewController upgradeMemberToNormal:userID];
    else
    {
        [self.meetingViewController showMessageAlert:@"确认将成员降级为观察者?" title:@"降级" completedBlock:^(BOOL selected) {
            if (selected)
                [weakSelf.meetingViewController degradeMemberToObserver:userID];
        }];
    }
}

- (IBAction)kickOutButtonPressed:(NSButton *)sender
{
    DLog(@"LLH...... kickOutButtonPressed");
    __weak typeof(self) weakSelf = self;
    NSString *userID = self.dataDic[kUserID];
    [self.meetingViewController showMessageAlert:@"确认将成员移出会议" title:@"移出会议" completedBlock:^(BOOL selected) {
        if (selected)
            [weakSelf.meetingViewController removeAttendedMember:userID];
    }];
}

- (void)mouseEntered:(NSEvent *)event
{
    
    if (!isMouseEntered)
    {
        isMouseEntered = YES;
        isMouseExited = NO;
        if (self.isHost && ![self.dataDic[kUserID] isEqualToString:self.meetingViewController.userID])
        {
            if ([self.dataDic[kUserType] integerValue] == RongRTC_User_Observer)
            {
                self.micButton.hidden = YES;
                self.cameraButton.hidden = YES;
            }
            else
            {
                self.micButton.hidden = NO;
                self.cameraButton.hidden = NO;
            }
            
            self.upgradeButton.hidden = NO;
            self.kickOutButton.hidden = NO;
        }
        else
        {
            self.micButton.hidden = YES;
            self.cameraButton.hidden = YES;
            self.upgradeButton.hidden = YES;
            self.kickOutButton.hidden = YES;
        }
        [self setNeedsDisplay:YES];
    }
}

- (void)mouseExited:(NSEvent *)event
{
    if (!isMouseExited)
    {
        isMouseEntered = NO;
        isMouseExited = YES;
    
        if (self.isHost)
        {
            self.micButton.hidden = YES;
            self.cameraButton.hidden = YES;
            self.upgradeButton.hidden = YES;
            self.kickOutButton.hidden = YES;
        }
        [self setNeedsDisplay:YES];
    }
}

@end
